package edu.gatech.bsbmule.view;

import javax.swing.JPanel;

import java.awt.GridLayout;

import javax.swing.JLabel;
import javax.swing.SwingConstants;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.xy.XYDataset;

import java.awt.Font;
import java.awt.GridBagLayout;
import javax.swing.JButton;
import java.awt.GridBagConstraints;
import java.awt.Insets;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class StatisticsScreen extends JPanel {
	
	/**
	 * Create the panel.
	 */
	public StatisticsScreen(XYDataset dataset) {
		GridBagLayout gridBagLayout = new GridBagLayout();
		gridBagLayout.columnWidths = new int[]{0, 0};
		gridBagLayout.rowHeights = new int[]{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
		gridBagLayout.columnWeights = new double[]{1.0, Double.MIN_VALUE};
		gridBagLayout.rowWeights = new double[]{1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
		setLayout(gridBagLayout);
		
		JPanel panel = plot(dataset);
		GridBagConstraints gbc_panel = new GridBagConstraints();
		gbc_panel.gridheight = 6;
		gbc_panel.insets = new Insets(0, 0, 5, 0);
		gbc_panel.fill = GridBagConstraints.BOTH;
		gbc_panel.gridx = 0;
		gbc_panel.gridy = 0;
		add(panel, gbc_panel);
		
		JButton btnReturnButton = new JButton("Return");
		btnReturnButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				MainWindow.getInstance().setPanel(EndOfGameScreen.getInstance());
			}
		});
		btnReturnButton.setFont(new Font("Tahoma", Font.PLAIN, 24));
		GridBagConstraints gbc_btnReturnButton = new GridBagConstraints();
		gbc_btnReturnButton.fill = GridBagConstraints.BOTH;
		gbc_btnReturnButton.gridheight = 4;
		gbc_btnReturnButton.insets = new Insets(0, 0, 5, 0);
		gbc_btnReturnButton.gridx = 0;
		gbc_btnReturnButton.gridy = 6;
		add(btnReturnButton, gbc_btnReturnButton);
		
	}
	
	private ChartPanel plot(XYDataset dataset) {
		JFreeChart chart = ChartFactory.createXYLineChart(
				"By score",
				"Round",
				"Score",
				dataset,
				PlotOrientation.VERTICAL,
				true,
				true,
				false);
		return new ChartPanel(chart);
	}
	
}
